package com.core.lib_core.fragment;

import android.view.LayoutInflater;
import android.view.View;

import com.core.lib_core.R;
import com.core.lib_core.activity.BaseActivity;
import com.core.lib_core.widgets.Alert;
import com.core.lib_core.widgets.viewhelper.VaryViewHelper;

/**
 * 用法同 {@link BaseActivity} 一样
 *
 * @author 李澄锋<br>
 * 2014年9月19日下午3:42:50
 */
public abstract class BaseFragment extends FrameBaseFragment {

    public VaryViewHelper mVaryViewHelper;

    public abstract String getFragmentName();

    @Override
    protected void findViewById() {
        super.findViewById();
        View contentView = findViewById(R.id.core_content_layout);
        if (contentView != null) {
            initVaryView();
        }
    }

    protected void initVaryView() {
        if (loadingview() <= 0 || emptyview() <= 0 || errorview() <= 0) {
            return;
        }
        mVaryViewHelper = new VaryViewHelper.Builder()
                .setDataView(findViewById(R.id.core_content_layout))//放数据的父布局，逻辑处理在该Activity中处理
                .setLoadingView(LayoutInflater.from(getActivity()).inflate(loadingview(), null))//加载页，无实际逻辑处理
                .setEmptyView(LayoutInflater.from(getActivity()).inflate(emptyview(), null))//空页面，无实际逻辑处理
                .setErrorView(LayoutInflater.from(getActivity()).inflate(errorview(), null))//错误页面
                .setRefreshListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        reloadData();
                    }
                })//错误页点击刷新实现
                .build();
    }

    public abstract int loadingview();

    public abstract int emptyview();

    public abstract int errorview();

    public void reloadData() {
        if (mVaryViewHelper != null) {
            mVaryViewHelper.showLoadingView();
        }
    }

    public boolean isEmpty(Object str) {
        if (str == null || str.toString().trim().length() == 0) {
            return true;
        } else {
            return false;
        }
    }

    public void showToast(String content) {
        Alert.toast(content);
    }

    @Override
    public void onDestroy() {
        if (mVaryViewHelper!=null){
            mVaryViewHelper = null;
        }
        super.onDestroy();
    }
}
